package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ClipView01
* */
class ClipView01 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
            color = context.resources.getColor(R.color.color_48D6AE)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            //strokeJoin = Paint.Join.ROUND
            //style = Paint.Style.STROKE
            //strokeWidth = mStrokeWidth
            color = context.resources.getColor(R.color.color_48D6AE)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            mPath0.apply {
                val baseX = cx * 1 / 5
                val baseY = cy * 1 / 5
                /*addArc(
                    -baseX,
                    -baseY,
                    baseX,
                    baseY,
                    0F,
                    360F,
                )*/

                /*addRect(
                    -baseX,
                    -baseY,
                    baseX,
                    baseY,
                    Path.Direction.CW
                )*/

                addArc(
                    -cx,
                    -cy * 2 + cy * 13 / 25,
                    cx,
                    0F + cy * 13 / 25,
                    0F,
                    360F
                )
            }

            mPath1.apply {
                /*val baseX = cx * 9 / 10
                val baseY = cy * 9 / 10
                addArc(
                    -baseX,
                    -baseY,
                    baseX,
                    baseY,
                    0F,
                    360F,
                )*/

                addArc(
                    -cx,
                    0F - cy * 13 / 25,
                    cx,
                    cy * 2 - cy * 13 / 25,
                    0F,
                    360F
                )
                clipPath(mPath0)
            }
            drawPath(mPath1, mPaint1)
        }
    }
}